package dao1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class PersonDao {
	public String insert(StudentModel model) throws Exception {// add save
		String sql = "insert into student1(id,name,year,gender,speciality)values(?,?,?,?,?)";
		Connection conn = JDBCUtil.getConnection();
		if (conn == null)
			return null;
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, model.getId());
			ps.setString(2, model.getName());
			ps.setInt(3, model.getYear());
			ps.setString(4, model.getGender());
			ps.setString(5, model.getSpeciality());
			int res = ps.executeUpdate();
			return res + "";
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}

	public String deleteById(StudentModel model) throws Exception {
		String sql = "delete from student where id = ?";
		Connection conn = JDBCUtil.getConnection();
		if (conn == null)
			return null;
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, model.getId());
			return ps.executeUpdate() + "";
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}
	public String update(StudentModel model) throws Exception {
        String sql = "update student set name=?,year=?,gender=?,speciality=? where id=?";
        Connection conn = JDBCUtil.getConnection();
        if (conn == null)
            return null;
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, model.getName());
            ps.setInt(2, model.getYear());
            ps.setString(3, model.getGender());
            ps.setString(4, model.getSpeciality());
            ps.setString(5, model.getId());
            return ps.executeUpdate() + "";
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn, ps);
        }
        return null;
    }
	public ArrayList<StudentModel> selectList(StudentModel model) throws Exception {
		StringBuffer sql = new StringBuffer("select id,name,year,gender,speciality from student ");
		List<Object> vals = appendWhere(sql, model);
		Connection conn = JDBCUtil.getConnection();
		if (conn == null)
			return null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ArrayList<StudentModel> list = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++)
				ps.setObject(i + 1, vals.get(i));
			rs = ps.executeQuery();
			StudentModel p = null;
			while (rs.next()) {// ORM
				p = new StudentModel();
				p.setId(rs.getString("id"));
				p.setName(rs.getString("name"));
				p.setYear(rs.getInt("year"));
				p.setGender(rs.getString("gender"));
				p.setSpeciality(rs.getString("speciality"));
				list.add(p);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;
	}

	private List<Object> appendWhere(StringBuffer sql, StudentModel model) {
		sql.append(" where 1=1 ");
		List<Object> vals = new ArrayList<>();
		
		String code = model.getSpeciality();
		if (code != null && !code.trim().isEmpty()) {
			sql.append(" and speciality = ? ");
			vals.add(code.trim());
		}
		String name = model.getName();
		if (name != null && !name.trim().isEmpty()) {
			sql.append(" and name = ? ");
			vals.add(name.trim());
		}
		String id = model.getId();
		if (id != null && !id.trim().isEmpty()) {
			sql.append(" and id = ? ");
			vals.add(id.trim());
		}
		
		return vals;
	}
}
